import json
from pathlib import Path
import httpx


vtb_path = Path(__file__).absolute().parent / 'vtbs.json'

with open(vtb_path,encoding='utf-8') as f:
    vtbs_data = json.load(f)

def find_vtb(id):
    resp = httpx.get(
        'https://account.bilibili.com/api/member/getCardByMid?mid={}'.format(id),
        timeout=10,
    )
    ret = resp.json()
    if ret['code'] != 0:
        return
    card = ret['card']
    vtbs = []
    for mid in card['attentions']:
        mid = str(mid)
        if mid in vtbs_data:
            vtb = vtbs_data[mid]
            vtbs.append(f"{vtb['uname']}({mid})")
    vtb_count = len(vtbs)
    vtb_msg = '、'.join(vtbs)
    print(f"{card['name']}({card['mid']})关注了 {vtb_count} 个vtb:\n{vtb_msg}")
    

while 1:
    find_vtb(input('>>'))
